﻿using System.Linq.Expressions;
using Microsoft.EntityFrameworkCore;
using PoemGame.Domain.GameAggregate;

namespace PoemGame.Repository.EF
{
    /// <summary>
    /// 游戏存储库
    /// 每次操作都写入数据库
    /// </summary>
    public class GameRepository:IGameRepository
    {
        private readonly PoemGameDbContext dbContext;
        public GameRepository(PoemGameDbContext dbContext)
        {
            this.dbContext = dbContext;
        }
        public async Task<Game?> GetAsync(Guid id)
        {
            return await dbContext.Games.FindAsync(id);
        }

        public async Task<Guid> AddAsync(Game game)
        {
            await dbContext.Games.AddAsync(game);
            await dbContext.SaveChangesAsync();
            return game.Id;
        }

        public async Task UpdateAsync(Game game)
        {
             dbContext.Update(game) ;
             await dbContext.SaveChangesAsync();
        }

        public async Task RemoveAsync(Game game)
        {
            dbContext.Remove(game);
            await dbContext.SaveChangesAsync();
        }

        public async Task<IEnumerable<Game>> GetAllAsync()
        {
            return await dbContext.Games.ToListAsync(); ;
        }

        public async Task<IEnumerable<Game>> GetByConditionAsync(Expression<Func<Game, bool>> predicate)
        {
            return await dbContext.Games.Where(predicate).ToListAsync();
        }
    }
}